#include "stdafx.h"
#include "ImageIO.h"
#include "ImageBasic.h"
#include "Segmentation.h"
#include "Tips.h"
#include "Shrink.h"
#include "Nodes.h"
#include "Graph.h"
#include <fstream>

IplImage* rawImage;
IplImage* binaryImage;
IplImage* skinImage;
IplImage* structureImage;


queue<point*> tipList;

bool* edgeHash;
bool* tipHash;

int THRESHOLD = 175;
double AREA_RATIO = 0.45;
int MODEL_HEIGHT = 5;
int MODEL_WIDTH = 5;
int MERGE_HEIGHT = 11;
int MERGE_WIDTH = 11;
int CENTERX = 620;
int CENTERY = 433;

vector<Nodes*> connectionList;

void test()
{
	clock_t start = clock();
	clock_t Start = start;
	doSegmentation();
	cvSaveImage("../segmentation.bmp",binaryImage);
	clock_t finish = clock();
	cout<<"Time spent on segmentation "<< finish - start <<" ms."<<endl<<endl;

	doShrinking();
	start = clock();
	cout<<"Time spent on shrinking "<<start - finish <<" ms."<<endl<<endl;
	//cvNamedWindow("Image",1);
	//cvShowImage("Image", skinImage);
	//cvWaitKey(0);
	cout<<"Searching for nodes..."<<endl;
	finish = clock();
	searchNodes(binaryImage, skinImage, structureImage);
	start = clock();
	cout<<"Searching for nodes completed."<<endl;
	cout<<"Time spent on searching nodes "<<start - finish<<" ms."<<endl<<endl;

	cout<<"Searching for graph connections..."<<endl;
	finish = clock();
	searchGraphConnection();
	saveGraphConnection();
	start = clock();
	cout<<"Searching fro graph connections completed."<<endl;
	cout<<"Time spent on searching graph connections "<<start - finish<<" ms."<<endl<<endl;


	cout<<"Saving images..."<<endl;
	cvSaveImage("../edge.bmp",skinImage);
	cvSaveImage("../structures.bmp", structureImage);
	saveRawStructureImage();
	finish = clock();
	cout<<"Saving images completed."<<endl;
	cout<<"Time spent on saving image "<<finish - start<<" ms."<<endl<<endl;

	clock_t Finish = finish;
	cout<<"Time spent in total "<<Finish - Start<<" ms."<<endl;

}
int main(int argc, char** argv)
{
	readInputParameters(argc, argv);

	binaryImage = cvCreateImage(cvGetSize(rawImage), IPL_DEPTH_8U, 1);
	skinImage = cvCreateImage(cvGetSize(rawImage), IPL_DEPTH_8U, 3);
	structureImage = cvCreateImage(cvGetSize(rawImage), IPL_DEPTH_8U, 3);

	edgeHash = new bool [rawImage->height*rawImage->width];
	tipHash  = new bool [rawImage->height*rawImage->width];

	test();

	return 0;
}

